package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;

/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-07
 */
@Service
public class NursingLevelServiceImpl implements INursingLevelService 
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;


    // 注入缓存redis模板 对象redisTemplate
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    // 定义的护理等级的 缓存变量
    private static final String KEY = "nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int i = nursingLevelMapper.insertNursingLevel(nursingLevel);
        deleteCache();
        return i;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int i = nursingLevelMapper.updateNursingLevel(nursingLevel);
        deleteCache();
        return i;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int i = nursingLevelMapper.deleteNursingLevelByIds(ids);
        deleteCache();
        return i;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        int i = nursingLevelMapper.deleteNursingLevelById(id);
        deleteCache();
        return i;
    }

    @Override
    public List<NursingLevelVo> listAll() {
        // 1, 先从缓存中 取，看能不能取到，能取到就直接返回，取不到就 从数据库中 查找
        List<NursingLevelVo> list =(List<NursingLevelVo>)redisTemplate.boundValueOps(KEY).get();

        // 2,判断有没有值
        if(list==null ){
        //     此时说明缓存中没有该数据 从数据库中 获取
            list= nursingLevelMapper.listAll();
        //     判断数据库中 有没有值，如果 有值 存到缓存中
            if(CollectionUtil.isNotEmpty(list)){
                redisTemplate.boundValueOps(KEY).set(list);
            }
        }
        // List<NursingLevelVo> list= nursingLevelMapper.listAll();
        return list;
    }


//     定义1个删除缓存的方法，在新增修改删除的时候要先删除缓存，等他下次查的时候，第一次走数据库，往后走缓存即可
    public void deleteCache(){
        redisTemplate.delete(KEY);
    }


}
